"""
Case Type   : delete
Case Name   : 列名大小写测试，delete using语句
Create At   : 2024/07/25
Owner       : songjing20
Description :
    1.兼容B库下建表
    2.插入数据
    3.删除重复数据，大写列名，查询
    4.删除重复数据，小写列名，查询
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.执行成功
    4.执行成功
    5.成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestDelete(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_delete_0022'
        self.tb_name = 't_delete_0022'
        self.log.info('-----创建B模式数据库-----')
        create_db_res = self.pri_sh.execut_db_sql(
            f"drop database if exists {self.db_name};"
            f"create database {self.db_name} dbcompatibility 'B';")
        self.log.info(create_db_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_db_res,
                      "建库失败")

    def test_delete(self):
        text = '-----step1:兼容B库下建表;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execut_db_sql(
            f"drop table if exists {self.tb_name} cascade;"
            f"create table {self.tb_name}("
            f"ID serial primary key, FRUIT varchar(50) not null);",
            dbname=self.db_name)
        self.log.info(res)
        self.assertEqual(res.count(self.constant.CREATE_TABLE_SUCCESS),
                         3, f'创建表失败: {text}')

        text = '-----step2:插入数据;expect:执行成功-----'
        self.log.info(text)
        sql = f"insert {self.tb_name}(FRUIT) values('apple');" \
              f"insert {self.tb_name}(FRUIT) values('apple');" \
              f"insert {self.tb_name}(FRUIT) values('orange');" \
              f"insert {self.tb_name}(FRUIT) values('orange');" \
              f"insert {self.tb_name}(FRUIT) values('orange');" \
              f"insert {self.tb_name}(FRUIT) values('banana');"
        sql_res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual(sql_res.count(self.constant.INSERT_SUCCESS_MSG),
                         6, f'执行失败: {text}')

        text = '-----step3:删除重复数据，大写列名，查询;expect:执行成功-----'
        self.log.info(text)
        res = self.pri_sh.execut_db_sql(
            f"delete from {self.tb_name} a using {self.tb_name} b "
            f"where a.ID<b.ID and a.FRUIT=b.FRUIT;"
            f"select * from {self.tb_name} order by 1;", dbname=self.db_name)
        self.log.info(res)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, res, f'执行失败: {text}')
        self.assertIn('2 | apple\n  5 | orange\n  6 | banana\n(3 rows)',
                      res, f'执行失败: {text}')

        text = '-----step4:删除重复数据，小写列名，查询;expect:执行成功-----'
        self.log.info(text)
        res = self.pri_sh.execut_db_sql(
            f"delete from {self.tb_name} a using {self.tb_name} b "
            f"where a.id>=b.id and a.fruit=b.fruit;"
            f"select * from {self.tb_name} order by 1;", dbname=self.db_name)
        self.log.info(res)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, res, f'执行失败: {text}')
        self.assertIn('(0 rows)', res, f'执行失败: {text}')

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop database if exists {self.db_name};")
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res,
                      f'删库失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
